package everyday;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/6/29 20:52
 */
public class Solution643 {

    // 滑动窗口 + 求最大平均值即求最大 Sum
    public double findMaxAverage(int[] nums, int k) {
        int n = nums.length;
        int sum = 0;
        // 初始化滑动窗口
        for (int i = 0; i < k; ++i) {
            sum += nums[i];
        }
        int maxSum = sum;  // 维护最大和
        // 滑动窗口
        for (int i = k; i < n; ++i) {
            sum = sum - nums[i - k] + nums[i];
            maxSum = Math.max(maxSum, sum);
        }
        return maxSum / (k * 1.0);
    }

    // 测试
    public static void main(String[] args) {
        int[] nums = new int[]{-1};
        int k = 1;
        double maxAverage = new Solution643().findMaxAverage(nums, k);
        System.out.println(maxAverage);
    }
}
